<template>
    <div v-if="visible" :class="[
        'loading-overlay',
        {
            'loading-overlay--fullscreen': fullscreen,
            'loading-overlay--backdrop': backdrop
        }
    ]">
        <div class="loading-content">
            <!-- 自定义插槽内容 -->
            <slot v-if="$slots.default"></slot>

            <!-- 默认加载内容 -->
            <template v-else>
                <div class="loading-spinner">
                    <ProgressSpinner v-if="type === 'spinner'" :style="{ width: size, height: size }" strokeWidth="4"
                        animationDuration="1s" />
                    <ProgressBar v-else-if="type === 'bar'" :value="progress" :showValue="showProgress"
                        class="loading-progress-bar" />
                    <div v-else-if="type === 'dots'" class="loading-dots">
                        <div class="dot"></div>
                        <div class="dot"></div>
                        <div class="dot"></div>
                    </div>
                    <div v-else-if="type === 'pulse'" class="loading-pulse">
                        <div class="pulse-circle"></div>
                    </div>
                </div>

                <div v-if="text" class="loading-text">
                    {{ text }}
                </div>

                <div v-if="description" class="loading-description">
                    {{ description }}
                </div>

                <!-- 取消按钮 -->
                <Button v-if="cancellable" label="取消" @click="handleCancel" severity="secondary" size="small"
                    class="loading-cancel-btn" />
            </template>
        </div>
    </div>
</template>

<script setup>
import Button from 'primevue/button';
import ProgressBar from 'primevue/progressbar';
import ProgressSpinner from 'primevue/progressspinner';

const props = defineProps({
    visible: {
        type: Boolean,
        default: false
    },
    type: {
        type: String,
        default: 'spinner', // 'spinner' | 'bar' | 'dots' | 'pulse'
        validator: (value) => ['spinner', 'bar', 'dots', 'pulse'].includes(value)
    },
    size: {
        type: String,
        default: '50px'
    },
    text: {
        type: String,
        default: ''
    },
    description: {
        type: String,
        default: ''
    },
    fullscreen: {
        type: Boolean,
        default: false
    },
    backdrop: {
        type: Boolean,
        default: true
    },
    progress: {
        type: Number,
        default: 0,
        validator: (value) => value >= 0 && value <= 100
    },
    showProgress: {
        type: Boolean,
        default: true
    },
    cancellable: {
        type: Boolean,
        default: false
    },
    zIndex: {
        type: Number,
        default: 9999
    }
});

const emit = defineEmits(['cancel']);

const handleCancel = () => {
    emit('cancel');
};
</script>

<style scoped>
.loading-overlay {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: v-bind('props.zIndex');
}

.loading-overlay--fullscreen {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
}

.loading-overlay--backdrop {
    background-color: rgba(0, 0, 0, 0.5);
    backdrop-filter: blur(2px);
}

.loading-content {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 2rem;
    background-color: var(--surface-card);
    border-radius: var(--border-radius);
    box-shadow: var(--shadow-4);
    max-width: 90vw;
    max-height: 90vh;
}

.loading-spinner {
    margin-bottom: 1rem;
}

.loading-progress-bar {
    width: 200px;
    margin-bottom: 1rem;
}

.loading-text {
    font-size: 1.125rem;
    font-weight: 500;
    color: var(--text-color);
    margin-bottom: 0.5rem;
    text-align: center;
}

.loading-description {
    font-size: 0.875rem;
    color: var(--text-color-secondary);
    margin-bottom: 1rem;
    text-align: center;
    line-height: 1.5;
}

.loading-cancel-btn {
    margin-top: 1rem;
}

/* 点状加载动画 */
.loading-dots {
    display: flex;
    gap: 0.25rem;
    margin-bottom: 1rem;
}

.loading-dots .dot {
    width: 8px;
    height: 8px;
    background-color: var(--primary-color);
    border-radius: 50%;
    animation: dot-bounce 1.4s ease-in-out infinite both;
}

.loading-dots .dot:nth-child(1) {
    animation-delay: -0.32s;
}

.loading-dots .dot:nth-child(2) {
    animation-delay: -0.16s;
}

.loading-dots .dot:nth-child(3) {
    animation-delay: 0s;
}

@keyframes dot-bounce {

    0%,
    80%,
    100% {
        transform: scale(0);
    }

    40% {
        transform: scale(1);
    }
}

/* 脉冲加载动画 */
.loading-pulse {
    position: relative;
    width: 50px;
    height: 50px;
    margin-bottom: 1rem;
}

.pulse-circle {
    position: absolute;
    width: 100%;
    height: 100%;
    border: 2px solid var(--primary-color);
    border-radius: 50%;
    animation: pulse-scale 1.5s ease-in-out infinite;
}

.pulse-circle::before {
    content: '';
    position: absolute;
    width: 100%;
    height: 100%;
    border: 2px solid var(--primary-color);
    border-radius: 50%;
    animation: pulse-scale 1.5s ease-in-out infinite;
    animation-delay: 0.75s;
    opacity: 0.5;
}

@keyframes pulse-scale {
    0% {
        transform: scale(0);
        opacity: 1;
    }

    100% {
        transform: scale(1);
        opacity: 0;
    }
}

/* 响应式设计 */
@media (max-width: 768px) {
    .loading-content {
        padding: 1.5rem;
        margin: 1rem;
    }

    .loading-progress-bar {
        width: 150px;
    }

    .loading-text {
        font-size: 1rem;
    }

    .loading-description {
        font-size: 0.8rem;
    }
}
</style>
